Articles Details
Technology: Java
Library: Jaxb
Type: tutorial
Background: Xml, Java.
JAXB helps java programmer manage in an easy way Xml file. In This article, we will learn how to use it to transform a Model class to an Xml File (Marshalling).
It will be possible to create element and put them in our model class instance and generate the xml file we need.
We will first see in more details how JAXB works and then we will create step by step an example of marshalling using JAXB.
JAXB
Xml (Extensible markup Language) is standard for exchanging data across systems. There are some API to manage Xml documents through Java Programming. One of them is Java Architecture for XML Binding (JAXB). It help the Java developper to easily transform Java classes to Xml document(Marshalling) and Xml document to Java Classes (Unmarshalling).
How does it work?
JAXB need an Annoted Object Class to produce An Xml document(Marshalling). JAXB transform an Xml document in an Annoted Object Class. An annoted Object Class is a Class which contains some annotations to map the Class to the Xml document structure. The information about the structure of Xml document is given by schema file(DTD, W3C shema…).To convert Xml schema or order schema file types to Java Annoted classes, JAXB use a tool named xjc. For the opposite operation JAXB use the shemagen tool.
Mini Tutorial
we will try to create a file cluster.xml which represents a cluster model from our Java class clusterImppl.java . A cluster contains some hosts which execute some jobs belonging to some jobGroups.
Step 1: Create the Java Annotated classes
Here is our Java class.
So we have 4 annotated classes:
ClusterImpl.java
@XmlRootElement(name = "cluster") @XmlAccessorType(XmlAccessType.NONE) public class ClusterImpl implements Cluster{ // attribute id String id; @XmlAttribute public String getId() { return id; } public void setId(String id) { this.id = id; } // attribute status String status; @XmlAttribute @Override public ClusterStatus getStatus() { // TODO Auto-generated method stub return ClusterStatus.OK; } public void setStatus(String status) { this.status = status; } // attribute master String master; @XmlAttribute public String getMaster() { return master; } public void setMaster(String master) { this.master = master; } // element Host == List of cluster host private List<HostImpl> hosts; @XmlElement(name="host") public List<HostImpl> getClusterHosts(){ if (hosts == null) { hosts = new ArrayList<HostImpl>(); } return this.hosts; } public void setClusterHosts(List<HostImpl> hosts) { this.hosts = hosts; } // element jobGroup == List of cluster job Groups private List<JobgroupImpl> jobGroups; @XmlElement(name="jobGroup") public List<JobgroupImpl> getClusterjobGroups(){ if (jobGroups == null) { jobGroups = new ArrayList<JobgroupImpl>(); } return this.jobGroups; } public void setClusterjobGroups(List<JobgroupImpl> jg) { this.jobGroups = jg; } // element job == List of cluster jobs private List<JobImpl> jobs; @XmlElement(name="job") public List<JobImpl> getClusterjobs(){ if (jobs == null) { jobs = new ArrayList<JobImpl>(); } return this.jobs; } public void setClusterjobs(List<JobImpl> j) { this.jobs = j; } ...
HostImpl.java
public class HostImpl implements Host{ String id; String type; String status; int memory; int load; public String getId() { return id; } @XmlAttribute public void setId(String id) { this.id = id; } public String getType() { return type; } @XmlAttribute public void setType(String type) { this.type = type; } public HostStatus getStatus() { return HostStatus.OK; } @XmlAttribute public void setStatus(String status) { this.status = status; } public int getMemory() { return memory; } @XmlAttribute public void setMemory(int memory) { this.memory = memory; } public int getLoad() { return load; } @XmlAttribute public void setLoad(int load) { this.load = load; } ...
JobGroupImpl.java
public class JobgroupImpl implements JobGroup { String id; public String getId() { return id; } @XmlAttribute public void setId(String id) { this.id = id; } public String getDescription() { return description; } @XmlElement public void setDescription(String description) { this.description = description; } String description; public JobgroupImpl() { // TODO Auto-generated constructor stub setId("default"); setDescription("this is the default group"); }
JobImpl.java
public class JobImpl implements Job { int id; public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } public String getStatus() { return status; } @XmlAttribute public void setStatus(String status) { this.status = status; } public Host getSubmissionHost() { HostImpl h = new HostImpl("Master"); return h; } @XmlAttribute public void setSubmissionHost(String submissionHost) { this.submissionHost = submissionHost; } public JobGroup getJobGroup() { JobgroupImpl jg = new JobgroupImpl(); return jg; } @XmlAttribute public void setJobGroup(String jobGroup) { this.jobGroup = jobGroup; } public Calendar getSubmissionTime() { SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm"); Date date = null; try { date = (Date) dateFormat.parse(submissionTime); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar cal=Calendar.getInstance(); cal.setTime(date); return cal; } @XmlAttribute public void setSubmissionTime(String submissionTime) { this.submissionTime = submissionTime; } String status; String submissionHost; String jobGroup; String submissionTime;
We use some annotation to let the JAXB know where is the root element (@XmlRootElement) ,Which field is an Xml element (@XmlElement) or an attribute (@XmlAttribute).
Step2: Using a JAXB instance
Now we will call Jaxb for marshalling job.
try { File file = new File(fileName); JAXBContext jaxbContext = JAXBContext.newInstance(ClusterImpl.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(cluster, file); jaxbMarshaller.marshal(cluster, System.out); } catch (JAXBException e) { e.printStackTrace(); } }
We create an istance of the Jaxb context using our annoted Class (which contains Root element) as argument.
Result
Here is the output:
Conclusion
In this article, we’ve covered the Marshalling of Java classes to Xml using the API JAXB in a simple way by the help of Jaxb annotation. Now if want to do more advanced operations with JAXB, I advise you some reading:
JAXB official page
Oracle article on JAXB
Wikipedia article on JAXB